package com.ljfresh.customer.erp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ljfresh.customer.utils.Dateutil;
import com.ljfresh.customer.utils.JacksonUtil;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

/**
 * @author: Lucifer
 * @time: 2020/4/13-10:09
 */
public class ERPParamBuilder {
    /**
     * 加签
     * @param map
     * @return
     */
        private static String sign(Map<String, Object> map, String secret){
        if(map==null){
            return null;
        }
        List<String> keyList = new ArrayList<>(map.keySet());
        Collections.sort(keyList);
        StringBuilder sb = new StringBuilder();
        sb.append(secret);
        for(int i=0;i<keyList.size();i++){
            String key = keyList.get(i);
            Object value = map.get(key);
            sb.append(key+value);
        }
        sb.append(secret);
        String sign = sb.toString();
        //System.out.println("before sign: "+sign);
        String md5Str = DigestUtils.md5Hex(sign);
        //System.out.println("after sign: "+md5Str);
        return md5Str;
    }


    /**
     * 加签  含&
     * @param map
     * @return
     */
    private static String signWithAnd(Map<String, Object> map, String secret){
        if(map==null){
            return null;
        }
        List<String> keyList = new ArrayList<>(map.keySet());
        Collections.sort(keyList);
        StringBuilder sb = new StringBuilder();
        sb.append(secret);
        Integer size = keyList.size();
        for(int i=0;i< size;i++){
            String key = keyList.get(i);
            Object value = map.get(key);
            sb.append(key+"="+value);
            if(i < size-1){
                sb.append("&");
            }
        }
        sb.append(secret);
        String sign = sb.toString();
        //System.out.println("before sign: "+sign);
        String md5Str = DigestUtils.md5Hex(sign);
        //System.out.println("after sign: "+md5Str);
        return md5Str;
    }

    /**
     * 请求参数拼接
     * @param map
     * @return
     */
    public static String paramStr(Map<String, Object> map){
        if(map==null){
            return null;
        }
        List<String> keyList = new ArrayList<>(map.keySet());
        Collections.sort(keyList);
        Integer size = keyList.size();
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<size;i++){
            String key = keyList.get(i);
            Object value = map.get(key);
            sb.append(key+"="+value);
            if(i < size-1){
                sb.append("&");
            }
        }
        return sb.toString();
    }


    /**
     * 通用参数生成(加签)并拼接
     * @param app_id    app_id
     * @param method    请求接口
     * @param secret    secret
     * @return
     */
    public static String generateParam(String app_id,String method,String secret){
        Map<String,Object> param = new HashMap<>();
        param.put("app_id",app_id);
        param.put("method",method);
        param.put("timestamp", Dateutil.toString(new Date(),Dateutil.NORMAL_FORMAT));
        //32位MD5加密
        String md5Str = sign(param,secret);
        param.put("sign",md5Str);
        return paramStr(param);
    }

}
